#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include"..//include//hash.h"




void hashfile(char file[],char path[])
{ 

char l;
int hash,c=1,l1;
l=tolower(file[0]);
if(isalpha(l))
{
hash=((int)l)-97;
}
else
{
hash=26;
}


l1=strlen(path);

if(path[l1-1]!='/')
{
strcat(path,"/");
}


if(1)              //CHECKING ALREADY IF THE FILE EXISTS
{
nodeh *temp;
temp=head[hash];
while(temp!=NULL&&temp->next!=NULL)
{
if((strcmp(temp->fn,file)==0)&&(strcmp(temp->path,path)==0))
{
return;
}
temp=temp->next;
}
if(temp!=NULL)
{
if((strcmp(temp->fn,file)==0)&&(strcmp(temp->path,path)==0))
{
return;
}
}

}



nodeh *p=(nodeh *)malloc(sizeof(nodeh));
p->next=NULL;
strcpy(p->fn,file);
strcpy(p->path,path);
strcpy(p->abspath,p->path);


//strcat(p->abspath,"/");           // add this to header file

strcat(p->abspath,p->fn);
//puts(p->abspath);
//puts("before if");
if(head[hash]==NULL)
{ 
head[hash]=p;
c--;
}
nodeh *temp=head[hash];
//puts("before while");
while(temp->next!=NULL)
{
temp=temp->next;
}
if(c!=0)
temp->next=p;

display(head[0]);
//puts("after while");
//return head[0];
}	

void display(nodeh *temp){
	while(temp!=NULL){
		//printf("XXX: %s ",temp->fn);
		temp=temp->next;
	}

}


void searchfileop(char file[])
{
char p[50][50],temp_str[20];
char l;
int hash,c=0,f,i=0,j=0;
l=tolower(file[0]);
if(isalpha(l))
{
hash=((int)l)-97;
}
else
{
hash=26;
}
nodeh *temp=head[hash];

while(temp->next!=NULL)
{ 
f=strcmp(temp->fn,file);
j=0;
if(f!=0)
{ 
while((temp->fn)[j]!='.')
{
temp_str[j]=(temp->fn)[j];
j++;
}
temp_str[j]='\0';
f=strcmp(temp_str,file);
}

if(f==0)
{ 
strcpy(p[i],temp->abspath);
//printf("Check: %s\n",temp->abspath);
i++;
f=1;
}
temp=temp->next;
}

f=strcmp(temp->fn,file);
j=0;
if(f!=0)
{
while((temp->fn)[j]!='.')
{
temp_str[j]=(temp->fn)[j];
j++;
}
temp_str[j]='\0';
f=strcmp(temp_str,file);
}

if(f==0)
{
strcpy(p[i],temp->abspath);
i++;
}
strcpy(p[i],"end");
if(strcmp(p[0],"end")==0)
{
pf("\n File not found");
}
else
{
i=0;
while((strcmp(p[i],"end"))!=0)
{
pf("\n File found at path:");
puts(p[i]);
i++;
}
}
//return 0;
}


void search_file_path(char filename[],char opfile[]){

char p[50][50],temp_str[20];
char l;
int len;
FILE *fp1;
len = strlen(filename);
int hash,c=0,f,i=0,j=0;
int flag;
flag=1;
l=tolower(filename[0]);
if(isalpha(l))
{
hash=((int)l)-97;
}
else
{
hash=26;
}
nodeh *temp=head[hash];

fp1=fopen(opfile,"wt");

while(temp!=NULL){
	flag  = 1;
	//strcpy(temp_str,"");	
	for(i=0;i<len;i++){
		if(temp->fn[i]!=filename[i]) flag = 0;
	
	}
	
	
	if(flag==1){
		strcpy(p[j],temp->abspath);
		j++;
	}
	
	temp=temp->next;

}

for(i=0;i<j;i++){
	fprintf(fp1,"%s\n",p[i]);

}

fclose(fp1);

}





void deletefile(char abspath[])
{
char file[10];
char l;
int hash,f=1,j,len;
len=strlen(abspath);
j=len-1;
while(abspath[j]!='/')
{
l=abspath[j];
//pf("\n l=%c",l);
j--;
}
//printf("First check in del hash\n");
l=tolower(l);
if(isalpha(l))
{
hash=((int)l)-97;
}
else
{
hash=26;
}
nodeh *prev,*temp,*t;
temp=head[hash];
prev=NULL;
if(temp->next==NULL)
{
head[hash]=NULL;
//pf("\nlist empty");
free(temp);
}
else
{
while((temp->next)!=NULL)
{

f=strcmp(temp->abspath,abspath);
if((f==0)&&(head[hash])==temp)
{
head[hash]=temp->next;
t=temp;
//free(t);
temp=NULL;
f=1;
break;
}
if(f==0)
{
prev->next=temp->next;
t=temp;
//free(t);
temp=NULL;
f=1;
break;
}

prev=temp;
temp=temp->next;
}


if(temp!=NULL)
{
f=strcmp(temp->abspath,abspath);

if((temp==head[hash])&&(f==0))
{
head[hash]==NULL;
//free(temp);
}

else if(f==0)
{
prev->next=NULL;
t=temp;
//free(t);
}
}

}
//printf("Last check in del hash\n");
//return 0;
}



void search(char name[])
{
char output[100][100];
int i,j,s,k=0;
nodeh *temp;
for(i=0;i<27;i++)
{
temp=head[i];
if(temp!=NULL)
{
while(temp->next!=NULL)
{
if(strstr(temp->fn,name))
{
printf("\nFile found at path:%s\n",temp->abspath);
strcpy(output[k],temp->abspath);
k++;
}
temp=temp->next;
}

if((temp->next==NULL)&&(strlen(temp->fn)>0))
{
if(strstr(temp->fn,name))
{
printf("\nFile found at path:%s\n",temp->abspath);
strcpy(output[k],temp->abspath);
k++;
}
}

}
}


//return 0;
}


